<?php
/**
 * Extractors Creation Screen
 * 
 * @author Xavier VILAIN
 * @package Orx\Etl
 * @license #license-orx#
 * 
 */
use Orx\Etl\Extractor\Source;
use Fox\Generator;
use Orx\Etl;
use Orx\Etl\Extractor;
use Fox\Html\Screen;

$xview = Xview::instance();

$screen = new Screen('sExtractor','create');
$screen->pageTitle(Msg(Etl::BUNDLE,Etl::EXTRACTOR_CREATION_TITLE));
$block = $xview->component('extractors','header');
$screen->add($block);
$sourceid = $block->item('source')->initialValue();
// Data Source Params
$blParams = $xview->component('extractors','datasource',array('source' => $sourceid));
$screen->add($blParams);

if($screen->submitted()) {
	$fields = $screen->get();
	if(!$screen->errors()) {
		try {
			// Parse Extractor Data Source Information
			$source = Fox\Dao::load(Source::className(),$fields['source']);
			$fields['params'] = $source->format($fields);
			$extractor = new Extractor();
			$extractor->setFields($fields);
			$extractor->create();
			Fox\Message::Success(Msg(Fox\Msg::BUNDLE,Fox\Msg::MSG_CREATE,$extractor->label()));
			$screen->redirect(Generator::url('extractors','update',null,array('extractor' => $extractor->getId())));
		}
		catch (\Fox\Object\Exception\Save $e) {
			Fox\Message::Error($e);
		}
		catch (\Fox\Object\Exception\NotExist $e) {
			Fox\Message::Error($e);
		}
	}
}
echo $screen->render();